package com.topscomm.mms;

import com.topscomm.cbo.pojo.CboApprovalNodeInsEntity;
import com.topscomm.cbo.service.ICboMessageAppService;
import com.topscomm.cbo.service.ICboMessageSysService;
import com.topscomm.cbo.service.impl.CboApprovalNodeInsService;
import com.topscomm.cbo.service.impl.CboUserService;
import com.topscomm.pub.util.*;
import com.topscomm.tap.ITapService;
import com.topscomm.tap.TapEntity;
import com.topscomm.tap.TapScript;
import com.topscomm.tap.common.DatacodeCache;
import com.topscomm.tap.common.TapSystemConst.TapApprovalNodeStateEnum;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: fanzhiming
 * @date: 2017年3月9日下午3:20:41
 * @description:
 * @modify:
 */
public class MmsScript extends TapScript {

    @Autowired
    private ICboMessageAppService cboMessageAppService;
    @Autowired
    private ICboMessageSysService cboMessageSysService;
    @Autowired
    private CboApprovalNodeInsService cboApprovalNodeInsService;
    @Autowired
    private CboUserService cboUserService;

    private static final String PROJECTSOURCETYPE="MpmProjectInfo";

    /**
     * 生成App消息推送内容
     *
     * @author: lanjianwei
     * @date: 2019年11月12日
     * @params:
     * @return_type: String
     * @description:
     * @modify:
     */
    public String geneAppContentSubmitNew(ITapService service, TapEntity entity) {
        // 审批节点实例
        CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
        // 组织系统消息内容
        StringBufferProxy msg = new StringBufferProxy();
        DatacodeCache datacodeCache = (DatacodeCache) SpringUtil.getBean("datacodeCache");
        String dispName = datacodeCache
            .getValueDynamic("CboApprovalFlowCategory", nodeins.getSourcetype(), "code", "name");
        String maintitle = nodeins.getMaintitle();
        String extendfield2 = nodeins.getExtendfield2();
        //maintitle只取时间字段
        if (!StringUtil.isEmpty(maintitle)) {
            int pos = maintitle.indexOf("于20");
            if (pos != -1) {
                maintitle = maintitle.substring(pos + 1, pos + 11);
            }
        }
        if (StringUtil.isEmpty(dispName)) {
            dispName = "业务申请";
        }
        if (nodeins.getState() == TapApprovalNodeStateEnum.UnApproved) {
            msg.appendSingle("您于{0}提交的{1}单据在“{2}”阶段被驳回！", maintitle, dispName, extendfield2);
        } else {
            msg.appendSingle("您于{0}提交的{1}单据在“{2}”阶段审批通过！", maintitle, dispName, extendfield2);
        }
        return msg.toString();
    }

    /**
     * 生成手机端系统消息主题
     *
     * @author: lanjianwei
     * @date: 2019年11月12日
     * @params:
     * @return_type: String
     * @description:
     * @modify:
     */
    public String geneSysSubjectSubmitNew(ITapService service, TapEntity entity) {
        StringBufferProxy msg = new StringBufferProxy();
        // 审批节点实例
        CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
        DatacodeCache datacodeCache = (DatacodeCache) SpringUtil.getBean("datacodeCache");
        String dispName = datacodeCache
            .getValueDynamic("CboApprovalFlowCategory", nodeins.getSourcetype(), "code", "name");
        if (StringUtil.isEmpty(dispName)) {
            dispName = "业务申请";
        }
        msg.appendSingle("{0}通知", dispName);
        return msg.toString();
    }

    /**
     * 生成手机端系统消息内容
     *
     * @author: lanjianwei
     * @date: 2019年11月12日
     * @params:
     * @return_type: String
     * @description:
     * @modify:
     */
    public String geneSysContentSubmitNew(ITapService service, TapEntity entity) {
        // 审批节点实例
        CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
        // 组织系统消息内容
        StringBufferProxy msg = new StringBufferProxy();
        DatacodeCache datacodeCache = (DatacodeCache) SpringUtil.getBean("datacodeCache");
        String dispName = datacodeCache
            .getValueDynamic("CboApprovalFlowCategory", nodeins.getSourcetype(), "code", "name");
        String maintitle = nodeins.getMaintitle();
        String extendfield2 = nodeins.getExtendfield2();
        //maintitle只取时间字段
        if (!StringUtil.isEmpty(maintitle)) {
            int pos = maintitle.indexOf("于20");
            if (pos != -1) {
                maintitle = maintitle.substring(pos + 1, pos + 11);
            }
        }
        if (StringUtil.isEmpty(dispName)) {
            dispName = "业务申请";
        }
        if (nodeins.getState() == TapApprovalNodeStateEnum.UnApproved) {
            msg.appendSingle("&nbsp;&nbsp;&nbsp;&nbsp;" + "您于{0}提交的{1}单据在“{2}”阶段被驳回！" + "<br>",
                maintitle, dispName, extendfield2);
        } else {
            msg.appendSingle("&nbsp;&nbsp;&nbsp;&nbsp;" + "您于{0}提交的{1}单据在“{2}”阶段审批通过！" + "<br>",
                maintitle, dispName, extendfield2);
        }
        String approvalopition = nodeins.getApprovalopinion();
        if (StringUtil.isEmpty(approvalopition)) {
            approvalopition = I18nUtil.getMessage("tap.common.not");
        }
        msg.appendSingle(
            "&nbsp;&nbsp;&nbsp;&nbsp;" + I18nUtil.getMessage("tap.cboapprovalnode.approvalcomments")
                + ":{0}<br>", approvalopition);
        msg.appendSingle(
            "&nbsp;&nbsp;&nbsp;&nbsp;" + I18nUtil.getMessage("tap.date.senddate") + ":{0}<br>",
            DateUtil.Now());
        return msg.toString();
    }



}
